package com.jsoft.service;

import com.jsoft.dto.SectorDto;
import com.jsoft.dto.UserDto;
import com.jsoft.manager.UserManager;
import com.jsoft.model.SectorDao;
import com.jsoft.model.UserDao;
import com.jsoft.utils.HttpSessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Date: 15.08.13
 * Time: 23:19
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserManager userManager;

    @Override
    @Transactional(readOnly = true)
    public UserDto getUserByLoginAndPassword(String login, String password) {
        UserDao userDao = userManager.getUserByLoginAndPassword(login, password);
        UserDto userDto = userDao.wrapUserDto();
        if (userDao.getPerson() != null) {
            userDto.setFirstName(userDao.getPerson().getFirstName());
            userDto.setSecondName(userDao.getPerson().getSecondName());
            userDto.setLastName(userDao.getPerson().getLastName());
        }
        if (userDto != null) {
            HttpSessionUtils.getSession().setAttribute("user", userDto);
        }
        return userDto;
    }

    @Override
    @Transactional(readOnly = true)
    public UserDto getUserByPassword(String password) {
        UserDao userDao = userManager.getUserByPassword(password);
        UserDto userDto = null;
        if (userDao != null) {
            userDto = userDao.wrapUserDto();
            if (userDao.getPerson() != null) {
                userDto.setFirstName(userDao.getPerson().getFirstName());
                userDto.setSecondName(userDao.getPerson().getSecondName());
                userDto.setLastName(userDao.getPerson().getLastName());
            }
            List<SectorDao> sectorDaoList = userDao.getSectorList();
            List<SectorDto> sectorDtoList = new ArrayList<SectorDto>();
            for (SectorDao sectorDao : sectorDaoList) {
                sectorDtoList.add(sectorDao.wrapSectorDto());
            }
            userDto.setSectorList(sectorDtoList);
            HttpSessionUtils.getSession().setAttribute("user", userDto);
        }
        return userDto;
    }

    @Override
    @Transactional(readOnly = true)
    public UserDto getUserByCard(String cardNumber) {
        UserDao userDao = userManager.getUserByCardNumber(cardNumber);
        UserDto userDto = userDao.wrapUserDto();
        if (userDao.getPerson() != null) {
            userDto.setFirstName(userDao.getPerson().getFirstName());
            userDto.setSecondName(userDao.getPerson().getSecondName());
            userDto.setLastName(userDao.getPerson().getLastName());
        }
        if (userDto != null) {                                                   //TODO может спринг секурити будет это делать
            HttpSessionUtils.getSession().setAttribute("user", userDto);
        }
        return userDto;
    }
}
